package com.ihr360.payroll.controller.salarytask.validator;

import com.ihr360.commons.lang.DateUtils;
import com.ihr360.payroll.entity.task.PayPeriodTermEntity;
import com.ihr360.shared.validator.SessionValidator;
import com.ihr360.validation.ValidationUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class PayPeriodTermValidator extends SessionValidator<PayPeriodTermEntity> {
    @Override
    public boolean supports(Class<?> clazz) {
        return super.supports(clazz) && PayPeriodTermEntity.class.equals(clazz);
    }

    @Override
    public void validate(PayPeriodTermEntity target) {
        super.validate(target);

        Date startOn = target.getStartOn();
        ValidationUtils.rejectIfEmpty(startOn, null, "必须设置账期批次中薪资周期的开始时间");

        Date endOn = target.getEndOn();
        ValidationUtils.rejectIfEmpty(endOn, null, "必须设置账期批次中薪资周期的结束时间");

        startOn = DateUtils.dateFormatYMD(startOn);
        endOn = DateUtils.dateFormatYMD(endOn);
        if (startOn.compareTo(endOn) >= 0) {
            ValidationUtils.reject(null, "账期批次中薪资周期的开始时间必须小于结束时间");
        }

        int workdays = target.getWorkdays();
        //多批次：额外校验工作天数
        if (!target.isPlanMonthCycle()) {
            ValidationUtils.rejectNotGt(workdays, 0, null, "工作日天数必须大于1");
        }

        int days = DateUtils.DateDiff(endOn, startOn);
        ValidationUtils.rejectNotLt(workdays, days, null, "工作日天数已超过账期批次中薪资周期的开始到结束时间的总天数");

        Date attendanceStartOn = target.getAttendanceStartOn();
        ValidationUtils.rejectIfEmpty(attendanceStartOn, null, "必须设置账期批次中考勤周期的开始时间");

        Date attendanceEndOn = target.getAttendanceEndOn();
        ValidationUtils.rejectIfEmpty(attendanceEndOn, null, "必须设置账期批次中考勤周期的结束时间");

        attendanceStartOn = DateUtils.dateFormatYMD(attendanceStartOn);
        attendanceEndOn = DateUtils.dateFormatYMD(attendanceEndOn);
        if (attendanceStartOn.compareTo(attendanceEndOn) >= 0) {
            ValidationUtils.reject(null, "账期批次中考勤周期的开始时间必须小于结束时间");
        }
    }
}